package com.huiyujiaoyu.powertime.ui.my;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.huiyujiaoyu.base.BaseActivity;
import com.huiyujiaoyu.http.HttpApi;
import com.huiyujiaoyu.http.HttpMethods;
import com.huiyujiaoyu.powertime.R;
import com.huiyujiaoyu.powertime.model.IndexItemModel;
import com.huiyujiaoyu.util.ActivityUtil;
import com.huiyujiaoyu.util.Constant;
import com.huiyujiaoyu.util.GlideUtil;
import com.huiyujiaoyu.util.StringUtils;
import com.huiyujiaoyu.widget.CustomLoadMoreView;
import com.huiyujiaoyu.widget.LlkjToolBar;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;


/**
 * Created by win7 on 2017/6/17.
 *  下拉刷新价值更多
 */

public class BaseListActivity extends BaseActivity implements SwipeRefreshLayout.OnRefreshListener {

    @BindView(R.id.toolbar)
    LlkjToolBar mToolbar;
    @BindView(R.id.recycle)
    RecyclerView mRvTwo;
    @BindView(R.id.swipe)
    SwipeRefreshLayout mSwipe;

    public static void launch(Context context, int type) {
        Bundle bundle = new Bundle();
        bundle.putInt(Constant.DATA, type);
        ActivityUtil.startActivity(context, BaseListActivity.class, bundle);
    }


    private List<IndexItemModel> ldata;
    private BaseQuickAdapter<IndexItemModel, BaseViewHolder> adapter;
    private int page = 1;
    private String title;
    private int type = 1;//我的订阅
    private int layId = 0;

    public void setTitleName() {
        switch (type) {
            case 1:
                title = "我的订阅";
                layId = R.layout.cell_subcribe_top;
                break;
            case 2:

                break;
            case 3:

                break;
        }
        setToolbar(mToolbar);
        mToolbar.setToolBar(title, true, "", R.mipmap.icon_titlebar_back, false, "", LlkjToolBar.isNull);
    }


    public void initView() {
        type = getIntent().getIntExtra(Constant.DATA,1);
        setTitleName();
        mRvTwo.setLayoutManager(new LinearLayoutManager(this));
        mRvTwo.setHasFixedSize(true);
        mSwipe
                .setColorSchemeResources(R.color.blue_,
                        R.color.blue_, R.color.blue_,
                        R.color.blue_);
        mSwipe.setOnRefreshListener(this);
    }

    public void initData() {
        ldata = new ArrayList<>();
        adapter = new BaseQuickAdapter<IndexItemModel, BaseViewHolder>(layId, ldata) {

            @Override
            protected void convert(BaseViewHolder helper, IndexItemModel s) {
                switch (type) {
                    case 1:
                        GlideUtil.loadRoundImg((ImageView) helper.getView(R.id.iv_cover), StringUtils.getFullUrl(s.cover));
                        helper.setText(R.id.tv_item_title,s.title);
                        helper.setText(R.id.tv_orderNum,s.subscribers+"人已订阅");
                        helper.setText(R.id.tv_updateNum,"更新"+s.upsum);
                        break;
                }
            }
        };
        mRvTwo.setAdapter(adapter);
        mRvTwo.addOnItemTouchListener(new OnItemClickListener() {
            @Override
            public void onSimpleItemClick(BaseQuickAdapter adapter, View view, int position) {
                switch (type) {
                    case 1:

                        break;
                    case 2:

                        break;
                    case 3:

                        break;
                }
            }
        });
        adapter.setLoadMoreView(new CustomLoadMoreView());
        mRvTwo.setAdapter(adapter);
        adapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                loadMore();
            }
        }, mRvTwo);
        mSwipe.setRefreshing(true);
        onRefresh();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.layout_titswirecy;
    }


    private void refresh() {
        if (mContext == null)
            return;
        if (adapter == null) {
            return;
        }
        if (adapter.isLoadMoreEnable()) {
            adapter.setEnableLoadMore(false);
        }
        page = 1;
        getData();
    }

    private void loadMore() {
        if (mSwipe.isEnabled()) {
            mSwipe.setEnabled(false);
        }
        page++;
        getData();
    }


    public void getData() {
        switch (type) {
            case 1:
                HttpMethods.getInstance().oColumn(mContext,getComp(),this,page);
                break;
        }
    }

    @Override
    public void onRefresh() {
        refresh();
    }

    @Override
    public void onNext(Object o, int httpcode) {
        super.onNext(o, httpcode);
        switch (httpcode) {
            case HttpApi.HTTP_COLUMN:
                doData(((List<IndexItemModel>) o));
                break;
        }
    }
    public void doData(List<IndexItemModel> data){
        if (page == 1) {
            ldata.clear();
            // 设置加载更多能用
            if (mSwipe.isRefreshing()) {
                mSwipe.setRefreshing(false);
            }
            ldata.addAll(data);
            adapter.notifyDataSetChanged();
            adapter.setEnableLoadMore(true);
            Log.e(TAG,"page == 1");
        }else{
            if (data == null||data.size() ==0 ) {
                adapter.loadMoreEnd();
            }else{
                ldata.addAll(data);
                adapter.notifyDataSetChanged();
                adapter.loadMoreComplete();
            }
            if(!mSwipe.isEnabled())
                mSwipe.setEnabled(true);
            Log.e(TAG,"page != 1");
        }
        Log.e(TAG,"page=="+page);
    }
}
